package org.yty.travelagencyredis.Dao.impl;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.yty.travelagencyredis.Dao.ChatUserDao;
import org.yty.travelagencyredis.model.Entity.ChatUser;

import java.util.ArrayList;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class ChatUserDaoImpl implements ChatUserDao {
    private final StringRedisTemplate redisTemplate;

    @Override
    public List<ChatUser> selectByIdList(List<String> chatUserIdList) {
        List<ChatUser> result = new ArrayList<>();
        for (String chatUserId : chatUserIdList){
            String chatUserJson = (String) this.redisTemplate.opsForHash().get(ChatUser.key, chatUserId);
            result.add(JSON.parseObject(chatUserJson, ChatUser.class));
        }
        return result;
    }

    @Override
    public ChatUser selectById(String id) {
        String chatUserJson = (String) this.redisTemplate.opsForHash().get(ChatUser.key, id);
        return JSON.parseObject(chatUserJson, ChatUser.class);
    }

    @Override
    public void deleteById(String id) {
        this.redisTemplate.opsForHash().delete(ChatUser.key, id);
    }
}
